package xin.nick.common.core.util;

import com.alibaba.fastjson2.JSON;
import org.springframework.http.MediaType;
import xin.nick.common.core.entity.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResultUtil {

    /**
     * 组装response返回Result
     *
     * @param response HttpServletResponse
     * @param result   Result
     * @throws IOException IOException
     */
    public static void responseResult(HttpServletResponse response, Result<?> result) throws IOException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        PrintWriter writer = response.getWriter();
        writer.println(JSON.toJSONString(result));
        writer.flush();
    }

}
